﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AuditModel;

namespace AuditWeb.Controllers
{ 
    public class CountyClassDateController : Controller
    {
        private AuditDBEntities db = new AuditDBEntities();
        IAnnualEventBL iaebl = new AnnualEventBL();
        IArchiveBL iabl = new ArchiveBL();
        //
        // GET: /CountyClassDate/

        public ViewResult Index()
        {
            AnnualEvent currentevent = iaebl.GetActiveEvents().FirstOrDefault();
            string annualeventid = currentevent.ID;
            
            var countyclassdates = db.CountyClassDates.Include("AnnualEvent").Include("County");
            var ccds = from ccd in countyclassdates
                       where ccd.AnnualEventID == annualeventid
                       select ccd;
            ViewBag.AnnualEvent = currentevent;
            ViewBag.SelectedIndex = "2";
            ViewBag.AnnualEventId = annualeventid;
            return View(ccds.ToList());
        }

        //
        // GET: /CountyClassDate/Details/5

        public ViewResult Details(string countycode,string date)
        {
            AnnualEvent currentevent = iaebl.GetActiveEvents().FirstOrDefault();
            string annualeventid = currentevent.ID;

            CountyClassDate countyclassdate = db.CountyClassDates.Single(c => c.CountyCode == countycode && c.AnnualEventID == annualeventid && c.ClassDate == DateTime.Parse(date));
            ViewBag.AnnualEvent = currentevent;
            ViewBag.SelectedIndex = "2";
            ViewBag.AnnualEventId = annualeventid;
            return View(countyclassdate);
        }

        //
        // GET: /CountyClassDate/Create

        public ActionResult Create()//the id is eventid
        {
            AnnualEvent currentevent = iaebl.GetActiveEvents().FirstOrDefault();
            string annualeventid = currentevent.ID;

            ViewBag.AnnualEventID = annualeventid;
            ViewBag.CountyCode = new SelectList(db.Counties, "Code", "Name");
            ViewBag.AnnualEvent = currentevent;
            ViewBag.SelectedIndex = "2";
            ViewBag.Id = annualeventid;
            return View();
        } 

        //
        // POST: /CountyClassDate/Create

        [HttpPost]
        public ActionResult Create(CountyClassDate countyclassdate)
        {
            if (ModelState.IsValid)
            {
                db.CountyClassDates.AddObject(countyclassdate);
                db.SaveChanges();
                SetArchivesClassDate(countyclassdate.AnnualEventID, countyclassdate.CountyCode, countyclassdate.ClassDate);
                return RedirectToAction("Index", new { id=countyclassdate.AnnualEventID});  
            }

            ViewBag.AnnualEventID = countyclassdate.AnnualEventID;
            ViewBag.CountyCode = new SelectList(db.Counties, "Code", "Name", countyclassdate.CountyCode);
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(countyclassdate.AnnualEventID);
            ViewBag.SelectedIndex = "2";
            ViewBag.Id = countyclassdate.AnnualEventID;
            return View(countyclassdate);
        }

        private void SetArchivesClassDate(string eventid,string countycode,DateTime date)
        {
            
            var archives = from a in db.Archives
                           where a.AnnualEventID == eventid && a.CountyCode == countycode
                           select a;
            foreach (var archive in archives)
            {
                archive.ClassDate = date;
                if (archive.ClassDate > archive.EvaluationStartDate)
                {
                    archive.EvaluationStartDate = date;
                    archive.EvaluationDueDate = date.AddDays(2);
                }
            }
            db.SaveChanges();

        }
        
        //
        // GET: /CountyClassDate/Edit/5
 
        public ActionResult Edit(string eventid, string countycode,string date)
        {
            
            CountyClassDate countyclassdate = db.CountyClassDates.Single(c => c.CountyCode == countycode && c.AnnualEventID == eventid && c.ClassDate == DateTime.Parse(date));

            ViewBag.AnnualEventID = countyclassdate.AnnualEventID;
            ViewBag.CountyCode = new SelectList(db.Counties, "Code", "Name", countyclassdate.CountyCode);
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(countyclassdate.AnnualEventID);
            ViewBag.SelectedIndex = "2";
            ViewBag.Id = countyclassdate.AnnualEventID;
            return View(countyclassdate);
        }

        //
        // POST: /CountyClassDate/Edit/5

        [HttpPost]
        public ActionResult Edit(CountyClassDate countyclassdate)
        {
            if (ModelState.IsValid)
            {
                db.CountyClassDates.Attach(countyclassdate);
                db.ObjectStateManager.ChangeObjectState(countyclassdate, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { id=countyclassdate.AnnualEventID});
            }

            ViewBag.AnnualEventID = countyclassdate.AnnualEventID;
            ViewBag.CountyCode = new SelectList(db.Counties, "Code", "Name", countyclassdate.CountyCode);
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(countyclassdate.AnnualEventID);
            ViewBag.SelectedIndex = "2";
            ViewBag.Id = countyclassdate.AnnualEventID;
            return View(countyclassdate);
        }

        //
        // GET: /CountyClassDate/Delete/5

        public ActionResult Delete(string eventid, string countycode, string date)
        {
            DateTime classdate = DateTime.Parse(date);
            CountyClassDate countyclassdate = db.CountyClassDates.Single(c => c.CountyCode == countycode && c.AnnualEventID == eventid && c.ClassDate == classdate);
            ViewBag.AnnualEventID = countyclassdate.AnnualEventID;
            ViewBag.CountyCode = new SelectList(db.Counties, "Code", "Name", countyclassdate.CountyCode);
            ViewBag.AnnualEvent = iaebl.GetAnnualEvent(countyclassdate.AnnualEventID);
            ViewBag.SelectedIndex = "2";
            ViewBag.Id = countyclassdate.AnnualEventID;
            return View(countyclassdate);
        }

        //
        // POST: /CountyClassDate/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string eventid, string countycode, string date)
        {
            DateTime classdate = DateTime.Parse(date);
            CountyClassDate countyclassdate = db.CountyClassDates.Single(c => c.CountyCode == countycode && c.AnnualEventID == eventid && c.ClassDate == classdate);
            db.CountyClassDates.DeleteObject(countyclassdate);
            db.SaveChanges();
            return RedirectToAction("Index", new { id = countyclassdate.AnnualEventID });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}